package cdn;

import groovy.lang.Closure;
import helper.GlobalConfig;
import helper.UrlHelper;
import org.apache.commons.lang.StringUtils;
import play.Play;
import play.templates.FastTags;
import play.templates.GroovyTemplate.ExecutableTemplate;

import java.io.PrintWriter;
import java.util.Map;

@FastTags.Namespace("asset")
public class AssetTag extends FastTags {

    // 以jvm启动时间作为PROD模式下的VERSION值.
    public static String PROD_VERSION = String.valueOf(System.currentTimeMillis());

    private static final String BASE_ASSET_URL = getBaseUrl();

    /**
     * 用法：
     * <p/>
     * <pre>
     *   #{asset.css '/main.css' /}
     * </pre>
     *
     * @param args
     * @param body
     * @param out
     * @param template
     * @param fromLine
     */
    public static void _css(Map<?, ?> args, Closure body, PrintWriter out,
                            ExecutableTemplate template, int fromLine) {
        //String postfixVersion = getPostfixVersion();
        String path = String.valueOf(args.get("arg"));
        start_css_tag(out);
        //out.print("href=\"" + BASE_ASSET_URL + path + "?" + postfixVersion + "\"");
        out.print("href=\"" + BASE_ASSET_URL + path + "\"");
        end_css_tag(out);
    }

    private static String getPostfixVersion() {
        return Play.mode.isDev() ? String.valueOf(System.currentTimeMillis()) : PROD_VERSION;
    }

    /**
     * 用法：
     * <p/>
     * <pre>
     *   #{asset.js '/a.js' /}
     * </pre>
     *
     * @param args
     * @param body
     * @param out
     * @param template
     * @param fromLine
     */
    public static void _js(Map<?, ?> args, Closure body, PrintWriter out,
                           ExecutableTemplate template, int fromLine) {
        //String postfixVersion = getPostfixVersion();
        String path = String.valueOf(args.get("arg"));
        start_js_tag(out);
        //out.print("src=\"" + BASE_ASSET_URL + path + "?" + postfixVersion + "\"");
        out.print("src=\"" + BASE_ASSET_URL + path + "\"");
        end_js_tag(out);
    }

    /**
     * 用法：
     * <p/>
     * <pre>
     *   #{asset.url "/test.png" /}
     * </pre>
     *
     * @param args
     * @param body
     * @param out
     * @param template
     * @param fromLine
     */
    public static void _url(Map<?, ?> args, Closure body, PrintWriter out,
                            ExecutableTemplate template, int fromLine) {
        //String postfixVersion = getPostfixVersion();
        String path = String.valueOf(args.get("arg"));
        //String split = path.indexOf('?') > 0 ? "&" : "?";
        //out.print(BASE_ASSET_URL + path + split + postfixVersion);
        out.print(BASE_ASSET_URL + path);
    }

    /**
     * 图片cdn标签, 带图像处理功能.
     *
     * 用法:
     *
     * <pre>
     *     #{asset.img "/u/20150930/001d02a1d8c0405984d273be2964d837.jpg", action: '100h_100w_0e', alt: 'hello'}
     * </pre>
     *
     * 可用action见: https://docs.aliyun.com/#/pub/oss/oss-img-api/image-processing&resize
     *
     * @param args
     * @param body
     * @param out
     * @param template
     * @param fromLine
     */
    public static void _img(Map<?, ?> args, Closure body, PrintWriter out,
                            ExecutableTemplate template, int fromLine) {
        //String postfixVersion = getPostfixVersion();
        String path = String.valueOf(args.get("arg"));
        start_img_tag(out);
        String imgSrc = UrlHelper.getUploadOssImageUrl(path, args.get("action") != null ? String.valueOf(args.get("action")) : null);
        out.print("src=\"" + imgSrc + "\"");
        if (args.get("alt") != null) {
            out.print(" alt=\"" + args.get("alt") + "\"");
        }
        end_img_tag(out);
    }

    /**
     * 图片cdn URL, 带图像处理功能.
     */
    public static void _imgUrl(Map<?, ?> args, Closure body, PrintWriter out,
                               ExecutableTemplate template, int fromLine) {
        //String postfixVersion = getPostfixVersion();
        String path = String.valueOf(args.get("arg"));
        String imgSrc = UrlHelper.getUploadOssImageUrl(path, args.get("action") != null ? String.valueOf(args.get("action")) : null);
        out.print(imgSrc);
    }

    private static String getBaseUrl() {
        String baseUrl = GlobalConfig.ASSETS_BASE_URL;
        String baseVersion = GlobalConfig.ASSETS_VERSION;
        if (StringUtils.isNotEmpty(baseVersion)) {
            baseUrl += "/" + baseVersion;
        }
        return baseUrl;
    }

    private static void end_css_tag(PrintWriter out) {
        out.print("/>");
    }

    private static void start_css_tag(PrintWriter out) {
        out.print("<link rel=\"stylesheet\" type=\"text/css\" "); // href="");
    }

    // <script language="javaScript" src="http://www.51cto.com/php/base_art.js"></SCRIPT>
    private static void start_js_tag(PrintWriter out) {
        out.print("<script type=\"text/javascript\" ");
    }

    private static void end_js_tag(PrintWriter out) {
        out.print(" charset=\"UTF-8\"></script>");
    }


    // <img src="http://image-demo.img-cn-hangzhou.aliyuncs.com/example.jpg@100h" alt="">
    private static void start_img_tag(PrintWriter out) {
        out.print("<img ");
    }
    private static void end_img_tag(PrintWriter out) {
        out.print("/>");
    }

}
